{extend name="public/father" /}

{block name='main'}
<div class="layui-card">
    <div class="layui-card-header">视频管理</div>
    <div class="layui-card-body">

<!--        <blockquote class="layui-elem-quote layui-quote-nm">-->
<!--            <p>标题中可以加入{city} 代表所在城市，{emoji} 代表随机表情</p>-->
<!--        </blockquote>-->
        <div style="margin: 15px 0;">
            <div class="layui-inline">
                <label class="layui-form-label">文章标题</label>
                <div class="layui-input-inline">
                    <input type="text" id="arttitle" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-primary" lay-filter="data-search-btn" id="searchBtn"><i class="layui-icon"></i> 搜 索</button>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">用户</label>
                <div class="layui-input-inline">
                    <input type="text" id="adminuser" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-primary" lay-filter="data-search-btn" id="searchBtnAdmin"><i class="layui-icon"></i> 搜 索</button>
            </div>
        </div>
        <div class="layui-btn-container">
            <button class="layui-btn" onclick="openUrl('添加', 'create', 800);return false;"><i class="layui-icon layui-icon-addition"></i>添加</button>
            <button class="layui-btn layui-btn-danger" onclick="openUrl('分享设置', '{:url(\'admin/config/set\')}?group=video', 800);return false;"><i class="layui-icon layui-icon-set"></i>分享设置</button>
            <button class="layui-btn layui-btn-danger" id="sync_video"><i class="layui-icon layui-icon-release"></i>同步视频</button>
            <button class="layui-btn" id="setkc"><i class="layui-icon layui-icon-set"></i>设置直跳链接</button>
            <button class="layui-btn" id="setxkc"><i class="layui-icon layui-icon-set"></i>设置多链接</button>
            <button class="layui-btn" id="setjx"><i class="layui-icon layui-icon-set"></i>设为精选</button>
            <button class="layui-btn" id="qxjx"><i class="layui-icon layui-icon-set"></i>取消精选</button>
            <button class="layui-btn" onclick="refreshTable('');return false;"><i class="layui-icon layui-icon-user"></i>查看所有用户</button>
            <button class="layui-btn layui-btn-primary" onclick="javascript:location.reload();"><i class="layui-icon layui-icon-ok-circle"></i> 刷新</button>
        </div>


        <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        <!-- 自定义adminname列的模板 -->  
        
        <script type="text/html" id="adminnameTpl">  
        
            <a href="javascript:;" onclick="reloadTable('{{d.admin}}')">{{d.adminname}}</a>  
        
        </script>
        <script type="text/html" id="status">
            {{ d.status==1 ? '已启用' : '<span style="color: orange;">已禁用</span>' }}
        </script>

       
        <script type="text/html" id="img">
            <img onclick="showImg(this.src);" src="{{ d.img }}" style="max-width: 100%;" alt="">
        </script>

        <script type="text/html" id="rukou">
            <div class="layui-btn-group">
                <a class="layui-btn layui-btn-sm layui-btn-primary" href="{:fission('https')}://{{d.domain}}/zhnw/{{ d.alias }}/{{d.admin}}/.html" target="_blank"><i class="layui-icon layui-icon-link"></i>打开</a>
                <a class="layui-btn layui-btn-sm layui-btn-primary" onclick="showImg('https://minico.qq.com/qrcode/get?type=2&r=2&size=200&text={:fission(\'https\')}://{{d.domain}}/zhnw/{{ d.alias }}/{{d.admin}}/.html')"><i class="layui-icon layui-icon-chart"></i>二维码</a>
                <a class="layui-btn layui-btn-sm layui-btn-primary" onclick="layer.alert('{:fission(\'https\')}://{{d.domain}}/zhnw/{{ d.alias }}/{{d.admin}}/.html', {title: false,shade: 0.5,closeBtn: 1,shadeClose: true,anim: 3,btn:false});"><i class="layui-icon layui-icon-search"></i>网址</a>
            </div>
        </script>

        <script type="text/html" id="is_jx">
            {{#  if(d.is_jx ==0){ }}
                <button class="layui-btn layui-btn-sm layui-btn-danger">否</button>
            {{# }if(d.is_jx ==1) { }}
                <button class="layui-btn layui-btn-sm">是</button>
            {{# } }}
        </script>

        <script type="text/html" id="barDemo">
            <div class="layui-btn-group">
                <a class="layui-btn layui-btn-sm layui-btn-primary" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
                <a class="layui-btn layui-btn-sm layui-btn-primary" onclick="openUrl('同步视频', '{:url(\'admin/video/user\')}?page=1&limit=100&video_ids={{d.id}}', 800);return false;"><i class="layui-icon layui-icon-release"></i>同步视频</a>
                <a class="layui-btn layui-btn-sm layui-btn-primary" lay-event="del" data-table="quns"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </div>
        </script>

    </div>
</div>
{/block}


{block name="js"}
<script>
    layui.use(['table', 'layer', 'form'], function() {
        var table = layui.table,
            layer = layui.layer,
            form = layui.form,
            $ = layui.jquery;

        table.render({
            elem: '#dataTable',
            url: "{:url()}",
            title: '数据表',
            size: 'lg',
            limit: 20,
            cols: [
                [
                    {type: "checkbox"},
                    {field: 'left', title: '操作', width: 225, align: 'center', toolbar: '#barDemo'},
                    {field: 'rukou', title: '入口网址', width: 235, align: 'center', templet: '#rukou'},
                    {field: 'title', title: '视频标题', minWidth: 230},
                    {field: 'is_jx', title: '是否精选', minWidth: 90, align: 'center', templet: '#is_jx'},
                    {field: 'miao', title: '秒', width: 60, sort: true},
                    {field: 'create_time', title: '发布时间', width: 160},
                    {field: 'img', title: '图片', width: 120, align: 'center', templet: '#img'},
                    {field: 'view', title: '访问量', width: 100, align: 'center', sort: true},
                    {field: 'adminname', title: '所属用户', width: 100, align: 'center', templet: '#adminnameTpl'}
                ]
            ],
            page: true, // 开启分页
            initSort: {
                field: 'id', // 默认排序字段
                type: 'desc' // 默认排序方式
            },
            where: {
                sortField: 'id', // 默认排序字段
                sortOrder: 'desc' // 默认排序方式
            },
            done: function(res, curr, count) {
                // done callback
            }
        });
        
        // 监听排序事件
        table.on('sort(dataTable)', function(obj) {
            // 根据排序字段和排序方式重新加载表格数据
            if(obj.field=="view"){
                table.reload('dataTable', {
                    where: {
                        sortField: obj.field, // 排序字段
                        sortOrder: obj.type // 排序方式
                    },
                    page: {
                        curr: 1 // 重新从第一页开始
                    }
                });
            }
            
        });

        // 新增reloadTable函数用于重新加载表格数据  

        window.reloadTable = function(adminname) {  

            table.reload('dataTable', {  

                url: "{:url()}&admin=" + encodeURIComponent(adminname), // 拼接URL参数  

                page: {  

                    curr: 1 // 重新从第一页开始  

                }  

            });  

        };  

        // 定义新的函数来刷新表格
        window.refreshTable = function() {
            table.reload('dataTable', {
                url: "{:url()}", // 拼接URL参数  
                page: {
                    curr: 1 // 重新从第一页开始
                }
            });
        };

        //设置直跳链接
        $("#setkc").on("click", function() {
            var checkStatus = table.checkStatus('dataTable');
            var ids = [];
            $(checkStatus.data).each(function (i, o) {
                ids.push(o.id);
            });
            if (ids.length < 1) {
                layer.msg('无选中项');
                return false;
            }
            ids = ids.join(",");
            layer.prompt({title: '请输入新的直跳链接', formType: 1}, function(val, index){
                $.ajax({
                    url: "/admin/video/updateUrl?type=zt",
                    type: "post",
                    data: {
                        "ids": ids,
                        "tourl": val
                    },
                    success:function(res){
                        layer.msg("设置新的直跳链接成功！")
                    }
                })
                layer.close(index);
            });
        });

        //设置返回链接(back_jump_url) 分享结束链接(share_end_url) 新链接跳转(new_shre_url)
        $("#setxkc").on("click", function() {
            var checkStatus = table.checkStatus('dataTable');
            var ids = [];
            $(checkStatus.data).each(function (i, o) {
                ids.push(o.id);
            });
            if (ids.length < 1) {
                layer.msg('无选中项');
                return false;
            }
            ids = ids.join(",");
            html = '<div class="layui-form-item">' +
                '  <label class="layui-form-label" style="width: 120px">新跳转链接</label>' +
                '  <div class="layui-input-block" style="margin-left: 150px">' +
                '    <input type="text" name="new_shre_url" required lay-verify="required" placeholder="请输入新跳转链接" autocomplete="off" class="layui-input">' +
                '  </div>' +
                '</div>'+
                '<div class="layui-form-item">' +
                '  <label class="layui-form-label" style="width: 120px">分享结束链接</label>' +
                '  <div class="layui-input-block" style="margin-left: 150px">' +
                '    <input type="text" name="share_end_url" required lay-verify="required" placeholder="请输入分享结束链接" autocomplete="off" class="layui-input">' +
                '  </div>' +
                '</div>'+
                '<div class="layui-form-item">' +
                '  <label class="layui-form-label" style="width: 120px">返回跳转链接</label>' +
                '  <div class="layui-input-block" style="margin-left: 150px">' +
                '    <input type="text" name="back_jump_url" required lay-verify="required" placeholder="请输入返回跳转链接" autocomplete="off" class="layui-input">' +
                '  </div>' +
                '</div>';
            layer.open({
                formType: 1,
                title: '链接设置',
                area: ['600px', '290px'], // 设置弹出层的宽度和高度
                btn: ['确认', '取消'],
                btnAlign: 'c',
                move: false,
                content: html,
                success: function(layero, index){},
                yes: function (index, layero) {
                    var newShreUrl = document.querySelector('input[name="new_shre_url"]').value;
                    var shareEndUrl = document.querySelector('input[name="share_end_url"]').value;
                    var backJumpUrl = document.querySelector('input[name="back_jump_url"]').value;
                    $.ajax({
                        url: "/admin/video/updateUrl?type=fh",
                        type: "post",
                        data: {
                            "ids": ids,
                            "new_shre_url": newShreUrl,
                            "share_end_url": shareEndUrl,
                            "back_jump_url": backJumpUrl
                        },
                        success:function(res){
                            layer.msg("设置链接成功！")
                        }
                    })
                    layer.close(index);
                },
            });
        });

        $("#setjx").on("click", function() {
            var checkStatus = table.checkStatus('dataTable');
            var ids = [];
            $(checkStatus.data).each(function (i, o) {
                ids.push(o.id);
            });
            if (ids.length < 1) {
                layer.msg('无选中项');
                return false;
            }
            ids = ids.join(",");
            //询问框

            layer.confirm('确定是否设置为精选文章？', {
              btn: ['确认','取消'] //按钮
            }, function(){
                $.ajax({
                    url: "/admin/video/setjx?type=set",
                    type: "post",
                    data: {
                        "ids": ids
                    },
                    success:function(res){
                        layer.msg("设置精选文章成功")
                    }
                })
            });
        });
        $("#qxjx").on("click", function() {
            var checkStatus = table.checkStatus('dataTable');
            var ids = [];
            $(checkStatus.data).each(function (i, o) {
                ids.push(o.id);
            });
            if (ids.length < 1) {
                layer.msg('无选中项');
                return false;
            }
            ids = ids.join(",");
            //询问框

            layer.confirm('确定是否取消精选文章？', {
              btn: ['确认','取消'] //按钮
            }, function(){
                $.ajax({
                    url: "/admin/video/setjx?type=qx",
                    type: "post",
                    data: {
                        "ids": ids
                    },
                    success:function(res){
                        layer.msg("设置精选文章成功")
                    }
                })
            });
        });
        $("#searchBtn").on("click", function() {
            let arttitle = $("#arttitle").val();
            table.reload("dataTable", {
                 where: {
                    "title": arttitle
                }
            });
        });
        $("#searchBtnAdmin").on("click", function() {
            let adminuser = $("#adminuser").val();
            table.reload("dataTable", {
                 where: {
                    "adminuser": adminuser
                }
            });
        })
        //同步
       $("#sync_video").on("click",function(){
           var checkStatus = table.checkStatus('dataTable');
           var ids = [];
           $(checkStatus.data).each(function (i, o) {
               ids.push(o.id);
           });
           if (ids.length < 1) {
               layer.msg('无选中项');
               return false;
           }
           ids = ids.join(",");
           openUrl('同步视频', '{:url(\'admin/video/user\')}?page=1&limit=100&video_ids='+ids, 800);
           return false;
       })
    });
</script>
{/block}
